package cn.meizhu.serve.controller;


import cn.meizhu.pet.domain.Pet;
import cn.meizhu.pet.query.PetQuery;
import cn.meizhu.pet.service.IPetService;
import cn.meizhu.serve.domain.Product;
import cn.meizhu.serve.query.ProductQuery;
import cn.meizhu.serve.service.IProductService;
import cn.meizhu.util.AjaxResult;
import cn.meizhu.util.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private IProductService productService;

    @PostMapping
    public QueryResult<Product> queryPage(@RequestBody ProductQuery ProductQuery) {
        return productService.queryPage(ProductQuery);
    }

    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> list) {
        try {
            productService.onsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> list) {

        try {
            productService.offsale(list);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }



    @PatchMapping
    public AjaxResult dels(@RequestBody Long[] ids){
        try {
            productService.dels(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @PutMapping
    public AjaxResult addAndUpd(@RequestBody Product product){
        try {
            if (product.getId()==null){
                productService.save(product);
            }else {
                productService.update(product);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/detail/{id}")
        public Product detail(@PathVariable("id") Long id){
        System.out.println(id);
        return productService.findByIdDetail(id);

    }

}
